/*************************************************
  File name     : xusbdeviceapi.h
  Module        : usb-static
  Author        : scar.chen
  Version       : 1.0
  Created on    : 2019-12-26
  Description   :
      usbdevice api(base on libusb1.0)

  Modify History:
  1. Date:        Author:         Modification:
*************************************************/
#ifndef XUSBDevAPI_H
#define XUSBDevAPI_H
#include "xusbdevicedef.h"
#ifdef __ANDROID__
    #include <jni.h>
#endif

/**
 * @file xusbdeviceapi.h
 * @brief x-usbdevice api.
 *
 */


#ifdef __cplusplus
extern "C"
{
#endif


/**
*   @brief  enum device
*   @param
*   name            |   type        |   param direction     |   description of param
*   ----------------|---------------|-----------------------|------------------------
*   vid             |   unsigned int|   [in]                |   vendor-id
*   pid             |   unsigned int|   [in]                |   product-id
*   devicelist      |   PXUSBDevice|    [out]               |   device list(alloc by caller)
*   nMaxCount       |   int         |   [in]                |   size
*   @return
*   describe of return value
*   value           |   type        |   description of value
*   ----------------|---------------|-------------------------------
*   >=0             |   int         |   device count
*   other           |   int         |   fail
*   @note
*
*/
int EKTPDEV_EnumDevice(unsigned short vid, unsigned short pid, PXUSBDevice deviceList, int nMaxCount);

/**
*   @brief  search device
*   @param
*   name            |   type        |   param direction     |   description of param
*   ----------------|---------------|-----------------------|------------------------
*   vid             |   unsigned int|   [in]                |   vendor-id
*   pid             |   unsigned int|   [in]                |   product-id
*   szSerialNumber  |   char*       |   [in]                |   serial number
*   device          |   PXUSBDevice|    [out]               |   device(alloc by caller)
*   @return
*   describe of return value
*   value           |   type        |   description of value
*   ----------------|---------------|-------------------------------
*   >=0             |   int         |   device count
*   other           |   int         |   fail
*   @note
*       Generally used for reconnect
*/
int EKTPDEV_SearchDevice(unsigned short vid, unsigned short pid, char *szSerialNumber, PXUSBDevice deviceInfo);
/**
*   @brief  open device
*   @param
*   name            |   type        |   param direction     |   description of param
*   ----------------|---------------|-----------------------|------------------------
*   device          |   PXUSBDevice|    [in]                |   device
*   @return
*       device handle
*   @note
*
*/
void *EKTPDEV_OpenDevice(PXUSBDevice usbDevice);

#ifdef __ANDROID__
/**
*   @brief  open device
*   @param
*   name            |   type        |   param direction     |   description of param
*   ----------------|---------------|-----------------------|------------------------
*   vid             |   unsigned int|   [in]                |   vendor-id
*   pid             |   unsigned int|   [in]                |   product-id
*   busNum          |   int         |   [in]                |   bus number
*   dev_address     |   int         |   [in]                |   device address
*   fd              |   int         |   [in]                |   file descriptor
*   @return
*       device handle(contain libusb context)
*   @note
*       only used on android
*/
void *EKTPDEV_OpenDevice_fd(int vid, int pid, int busNum, int dev_address, int fd);

/**
*   @brief  open device
*   @param
*   name            |   type        |   param direction     |   description of param
*   ----------------|---------------|-----------------------|------------------------
*   jvm             |   JavaVM*     |   [in]                |   JVM
*   env             |   JNIEnv*     |   [in]                |   JNIEnv
*   obj             |   jobject     |   [in]                |   XUSBHOSTAPIService
*   @return
*       device handle(contain libusb context)
*   @note
*       only used on android
*/
void *EKTPDEV_OpenDevice_obj(JavaVM *jvm, JNIEnv *env, jobject obj);
#endif

/**
*   @brief  close device
*   @param
*   name            |   type        |   param direction     |   description of param
*   ----------------|---------------|-----------------------|------------------------
*   handle          |   void*       |   [in]                |   device handle
*   @return
*   describe of return value
*   value           |   type        |   description of value
*   ----------------|---------------|-------------------------------
*   >=0             |   int         |   device count
*   other           |   int         |   fail
*   @note
*
*/
int EKTPDEV_CloseDevice(void *handle);

#ifdef __ANDROID__
/**
    *   @brief  close device
    *   @param
    *   name            |   type        |   param direction     |   description of param
    *   ----------------|---------------|-----------------------|------------------------
    *   jvm             |   JavaVM*     |   [in]                |   JVM
    *   env             |   JNIEnv*     |   [in]                |   JNIEnv
    *   handle          |   void*       |   [in]                |   device handle
    *   @return
    *   describe of return value
    *   value           |   type        |   description of value
    *   ----------------|---------------|-------------------------------
    *   >=0             |   int         |   device count
    *   other           |   int         |   fail
    *   @note
    *
    */
int EKTPDEV_CloseDevice_obj(JavaVM *jvm, JNIEnv *env, void *handle);
#endif


/**
*   @brief  clear halt and reset device
*   @param
*   name            |   type            |   param direction     |   description of param
*   ----------------|-------------------|-----------------------|------------------------
*   handle          |   void*           |   [in]                |   device handle
*   @return
*       nothing
*   @note
*       only reset usb host
*/
void EKTPDEV_ResetPort(void *handle);


int EKTPDEV_SCardTransmit(void *handle, int index, unsigned char *apduCommand, int cbApduCommand, unsigned char *apduResult, int *cbApduResult);


int EKTPDEV_IsCardPresent(void *handle, int index);

int EKTPDEV_Connect(void *handle, int index, unsigned char *atr, int *cbAtr);

int EKTPDEV_DisConnect(void *handle, int index);

#ifdef __cplusplus
};
#endif


#endif